package com.wowangz.cms.common.utils;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.wowangz.cms.syscore.exception.ServiceException;

/**
 * @author leiyang3
 *
 */
public class DataFormat {

	public static final int DT_MMDD = 10;// MMdd
	public static final int DT_SSS = 3; // SSS
	public static final int DT_YYIMMIDD = 11;// MMdd
	public static final int DT_YYMMDDHHMMSS = 13; // yyMMddHHmmss
	/**
	 * 日期格式常量
	 */
	public static final int DT_YYYY_MM_DD = 1; // yyyy-MM-dd
	public static final int DT_YYYYIMMIDD = 12;// MMdd
	public static final int DT_YYYYMMDD = 6; // yyyyMMdd
	public static final int DT_YYYYMMDD_HHMM = 9; // yyyy-MM-dd HH:mm
	public static final int DT_YYYYMMDD_HHMMSS = 8; // yyyy-MM-dd HH:mm:ss
	public static final int DT_YYYYMMDD_HHMMSS_SSSS = 5; // yyyy-MM-dd
	// HH:mm:ss.S
	public static final int DT_YYYYMMDDHHMMSS = 2; // yyyyMMddHHmmss

	public static String bSubString(String source, int beginIndex, int endIndex) {
		return bSubString(source, beginIndex, endIndex, -1);
	}

	public static String bSubString(String source, int beginIndex, int endIndex, int flag) {
		String returnStr = "";
		endIndex = endIndex - 1;
		if (source != null && beginIndex >= 0 && endIndex > 0 && (beginIndex <= endIndex)) {
			final byte[] bStr = source.getBytes();
			// System.out.println("bStr.length=" + bStr.length);
			if (beginIndex < bStr.length) {
				if (beginIndex > 0) {
					final String beginStr = new String(bStr, beginIndex - 1, 2);
					// System.out.println("beginStr=" + beginStr);
					if (beginStr.length() == 1 && source.contains(beginStr)) {
						beginIndex += flag;
					}
				}
				if (endIndex >= bStr.length - 1) {
					returnStr = new String(bStr, beginIndex, bStr.length - beginIndex);
				} else {
					final String endStr = new String(bStr, endIndex, 2);
					// System.out.println("endStr = " + endStr);
					if (endStr.length() == 1 && source.contains(endStr)) {
						endIndex += flag;
					}
					// System.out.println("beginIndex=" + beginIndex
					// +" endIndex=" + endIndex );
					returnStr = new String(bStr, beginIndex, endIndex - beginIndex + 1);

				}
			}

		}
		// System.out.println("returnStr=" + returnStr );

		return returnStr;
	}

	/*************
	 * 计算日期 加减 只针对 年 月 日
	 * 
	 * @param date
	 *            日期
	 * @param _date
	 *            加减数字
	 * @param type
	 *            1 年 2 月 3 天
	 * @return Date
	 * @author yubaode
	 */
	public static Date calculationSubtractionDate(Date date, int _date, int type) {
		Date cdate = DataFormat.parseDate(DataFormat.formatDate(date, DataFormat.DT_YYYY_MM_DD), DataFormat.DT_YYYY_MM_DD);
		Calendar cl = Calendar.getInstance();
		cl.setTime(cdate);
		if (type == 1) {
			cl.add(Calendar.YEAR, _date);
		} else if (type == 2) {
			cl.add(Calendar.MONTH, _date);
		} else if (type == 3) {
			cl.add(Calendar.DAY_OF_MONTH, _date);
		}
		return cl.getTime();
	}

	public static String formatAmount(double dValue) {
		return formatNumber(dValue, 2);
	}

	/**
	 * 格式化数字，例如：12345转化为12,345
	 *
	 * @param dValue
	 *            被格式化的数值
	 * @param iScale
	 *            小数点后保留位数,不足补0
	 * @return
	 */
	public static String formatAmount(double dValue, int iScale) {
		DecimalFormat df = null;
		final StringBuffer sPattern = new StringBuffer(",##0");
		String temp = "";
		if (iScale > 0) {
			sPattern.append(".");
			for (int i = 0; i < iScale; i++) {
				sPattern.append("0");
			}
		}
		/* if(dValue!=0.00){ */
		df = new DecimalFormat(sPattern.toString());
		temp = df.format(dValue);
		/*
		 * } else { temp = "0.00"; }
		 */

		return temp;
	}

	public static String formatDate(java.util.Date date, int type) throws ServiceException {
		if (date == null) {
			return "";
		}

		final SimpleDateFormat dateFormat = new SimpleDateFormat();
		dateFormat.applyPattern(DataFormat.getDateConvertString(type));
		return dateFormat.format(date);
	}

	/**
	 * 格式化数字，例如：将5转化为4位字符，则得到0005
	 *
	 * @param dValue
	 *            被格式化的数值
	 * @param nWidth
	 *            需要转换的位数
	 * @return
	 */
	public static String formatInt(long lValue, int nWidth) {
		String strReturn = "" + lValue;
		final int initLength = strReturn.length();
		for (int i = nWidth; i > initLength; i--) {
			strReturn = "0" + strReturn;
		}
		return strReturn;
	}

	/**
	 * 格式化数字，例如：12345转化为12345
	 *
	 * @param dValue
	 *            被格式化的数值
	 * @param iScale
	 *            小数点后保留位数,不足补0
	 * @return
	 */
	public static String formatNumber(double dValue, int iScale) {
		if (Double.isNaN(dValue)) {
			return "";
		}
		// if(dValue<0)
		// {
		// dValue=0;
		// }
		DecimalFormat df = null;
		final StringBuffer sPattern = new StringBuffer("##0");
		if (iScale > 0) {
			sPattern.append(".");
			for (int i = 0; i < iScale; i++) {
				sPattern.append("0");
			}
		}
		df = new DecimalFormat(sPattern.toString());
		return df.format(dValue);
	}

	/**
	 * 格式化序列号
	 *
	 * @param precode
	 *            固定字符前缀
	 * @param prefix
	 *            变动序列号前缀，长度为1
	 * @param serno
	 *            序列号数值
	 * @param iScale
	 *            变动序列号长度
	 * @return
	 */
	public static String formatNumberSerNo(String precode, String prefix, long serno, int iScale) {
		final StringBuffer s = new StringBuffer();
		s.append(precode.trim());

		if (serno >= 0 && iScale > 0) {
			final String temp = serno + "";
			final int len = temp.trim().length();
			final int clen = iScale - len;
			if (clen > 0) {
				for (int i = 0; i < clen; i++) {
					s.append(prefix.trim());
				}
				s.append(serno);
			} else if (clen == 0) {
				s.append(serno);
			} else {
				s.append(serno);
			}

		}

		return s.toString();
	}

	public static String formatString(String arg0) {
		if (arg0 == null) {
			return "";
		} else {
			return arg0;
		}
	}

	/**
	 * 获得系统当前月
	 *
	 * @return
	 */
	public static Integer getCurMonth() {
		Integer curMonth = 1;
		try {
			final SimpleDateFormat sdf = new SimpleDateFormat("MM");
			curMonth = new Integer(sdf.format(new Date()));
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return curMonth;
	}

	/**
	 * 获得系统当前年
	 *
	 * @return
	 */
	public static Integer getCurYear() {
		Integer curYear = 2009;
		try {
			final SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			curYear = new Integer(sdf.format(new Date()));
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return curYear;
	}

	/**
	 * 日期格式常量对应的字符串
	 *
	 * @param type
	 * @return
	 */
	public static String getDateConvertString(int type) {
		String strFormatString = "";
		switch (type) {
		case DataFormat.DT_YYYY_MM_DD:
			strFormatString = "yyyy-MM-dd";
			break;
		case DataFormat.DT_YYYYMMDDHHMMSS:
			strFormatString = "yyyyMMddHHmmss";
			break;
		case DataFormat.DT_SSS:
			strFormatString = "SSS";
			break;
		case DataFormat.DT_YYYYMMDD_HHMMSS_SSSS:
			strFormatString = "yyyy-MM-dd HH:mm:ss.S";
			break;
		case DataFormat.DT_YYYYMMDD:
			strFormatString = "yyyyMMdd";
			break;
		case DataFormat.DT_YYYYMMDD_HHMMSS:
			strFormatString = "yyyy-MM-dd HH:mm:ss";
			break;
		case DataFormat.DT_YYYYMMDD_HHMM:
			strFormatString = "yyyy-MM-dd HH:mm";
			break;
		case DataFormat.DT_MMDD:
			strFormatString = "MMdd";
			break;
		case DataFormat.DT_YYIMMIDD:
			strFormatString = "yy/MM/dd";
			break;
		case DataFormat.DT_YYYYIMMIDD:
			strFormatString = "yyyy/MM/dd";
			break;
		case DataFormat.DT_YYMMDDHHMMSS:
			strFormatString = "yyMMddHHmmss";
			break;
		default:
			strFormatString = "yyyy-MM-dd";
			break;
		}
		return strFormatString;
	}
	
	/**
	 * 日期格式常量对应的字符串
	 * @param tsVar
	 * @param type
	 * @return
	 */
	public static String getDateConvertString(Timestamp tsVar,int type) {
		String strFormatString = "";
		try{
			SimpleDateFormat formatter = new SimpleDateFormat();
			switch (type) {
			case DataFormat.DT_YYYY_MM_DD:
				formatter.applyPattern("yyyy-MM-dd");
				break;
			case DataFormat.DT_YYYYMMDDHHMMSS:
				formatter.applyPattern("yyyyMMddHHmmss");
				break;
			case DataFormat.DT_SSS:
				strFormatString = "SSS";
				break;
			case DataFormat.DT_YYYYMMDD_HHMMSS_SSSS:
				formatter.applyPattern("yyyy-MM-dd HH:mm:ss.S");
				break;
			case DataFormat.DT_YYYYMMDD:
				formatter.applyPattern("yyyyMMdd");
				break;
			case DataFormat.DT_YYYYMMDD_HHMMSS:
				formatter.applyPattern("yyyy-MM-dd HH:mm:ss");
				break;
			case DataFormat.DT_YYYYMMDD_HHMM:
				formatter.applyPattern("yyyy-MM-dd HH:mm");
				break;
			case DataFormat.DT_MMDD:
				formatter.applyPattern("MMdd");
				break;
			case DataFormat.DT_YYIMMIDD:
				formatter.applyPattern("yy/MM/dd");
				break;
			case DataFormat.DT_YYYYIMMIDD:
				formatter.applyPattern("yyyy/MM/dd");
				break;
			case DataFormat.DT_YYMMDDHHMMSS:
				formatter.applyPattern("yyMMddHHmmss");
				break;
			default:
				formatter.applyPattern("yyyy-MM-dd");
				break;
			}
			strFormatString = formatter.format(tsVar);
		}catch(Exception e){
			strFormatString = "";
		}
		return strFormatString;
	}


	/**
	 *
	 * @描述：获得当前日期的格式化
	 * @author HUANGYUAN
	 * @TIME:2015年8月25日 下午4:10:24
	 * @param type
	 * @return
	 */
	public static String getDateString(String type) {
		final SimpleDateFormat sdf = new SimpleDateFormat(type);
		return sdf.format(new Date());
	}

	/**
	 * convert Timestamp to string "yyyy-mm-dd"
	 *
	 * @param ts
	 * @return
	 */
	static public String getDateString(Timestamp ts) {
		if (null == ts) {
			return "";
		}
		final java.util.Calendar calendar = Calendar.getInstance();
		calendar.setTime(ts);
		String strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (strMonth.length() == 1) {
			strMonth = "0" + strMonth;
		}
		String strDay = String.valueOf(calendar.get(Calendar.DATE));
		if (strDay.length() == 1) {
			strDay = "0" + strDay;
		}
		return calendar.get(Calendar.YEAR) + "-" + strMonth + "-" + strDay;
	}

	/**
	 * convert Timestamp to string "YYYYMMDDHHMMSS"
	 *
	 * @param ts
	 * @return
	 */
	static public String getDateTimeString(java.sql.Timestamp ts) {
		if (null == ts) {
			return "";
		}
		final java.util.Calendar calendar = Calendar.getInstance();
		calendar.setTime(ts);
		/*
		 * return calendar.get ( Calendar.YEAR ) + "-" + (calendar.get (
		 * Calendar.MONTH ) + 1) + "-" + calendar.get ( Calendar.DATE ) + " " +
		 * calendar.get ( Calendar.HOUR_OF_DAY ) + ":" + calendar.get (
		 * Calendar.MINUTE ) + ":" + calendar.get ( Calendar.SECOND ) ;
		 */

		String strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		if (strMonth.length() == 1) {
			strMonth = "0" + strMonth;
		}
		String strDay = String.valueOf(calendar.get(Calendar.DATE));
		if (strDay.length() == 1) {
			strDay = "0" + strDay;
		}
		String strHour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		if (strHour.length() == 1) {
			strHour = "0" + strHour;
		}
		String strMinute = String.valueOf(calendar.get(Calendar.MINUTE));
		if (strMinute.length() == 1) {
			strMinute = "0" + strMinute;
		}
		String strSecond = String.valueOf(calendar.get(Calendar.SECOND));
		if (strSecond.length() == 1) {
			strSecond = "0" + strSecond;
		}
		return calendar.get(Calendar.YEAR) + "" + strMonth + "" + strDay + "" + strHour + "" + strMinute + "" + strSecond;
		// return ts.toString();
	}

	/**
	 *
	 * @描述 获得结束时间
	 * @author HUANGYUAN 2015年8月7日 下午12:27:05
	 *
	 * @param endTime
	 * @return
	 */
	public static String getEndtimetString(String endTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if (StringUtils.isEmpty(endTime)) {
				return "";
			}
			final Date date = format.parse(endTime);
			final long minis = date.getTime() + (59 * 60 + 59 + 23 * 60 * 60) * 1000;
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			endTime = format.format(new Date(minis));
			return endTime;
		} catch (final ParseException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * add by yoyo 上面的方法有问题，如果有时分秒，相邻的日期会计算成相差两天 新写一个方法
	 *
	 * @param tsBeginDate
	 * @param tsEndDate
	 * @return
	 */
	public static long getIntervalDay(Date tsBeginDate, Date tsEndDate) {

		long lInterval = -1;
		final Calendar calBegin = Calendar.getInstance();
		final Calendar calEnd = Calendar.getInstance();
		calBegin.setTime(tsBeginDate);
		calBegin.set(Calendar.HOUR_OF_DAY, 0);
		calBegin.set(Calendar.MINUTE, 0);
		calBegin.set(Calendar.SECOND, 0);
		calEnd.setTime(tsEndDate);
		calEnd.set(Calendar.HOUR_OF_DAY, 0);
		calEnd.set(Calendar.MINUTE, 0);
		calEnd.set(Calendar.SECOND, 0);

		final Timestamp begin = new Timestamp(calBegin.getTimeInMillis());
		final Timestamp end = new Timestamp(calEnd.getTimeInMillis());
		lInterval = end.getTime() - begin.getTime();
		lInterval = lInterval / (24 * 60 * 60 * 1000);

		return lInterval;

	}

	/**
	 * 开始计算实际间隔日期 add by Forest
	 *
	 * @return
	 */
	static public int getIntervalDays(java.util.Date tsStart, java.util.Date tsEnd) {
		int lIntervalDays = 0;

		while (!tsStart.after(tsEnd)) {
			tsStart = getNextDate(tsStart, 1);
			lIntervalDays++;
		}
		return lIntervalDays;
	}

	// 传入一个时间，得到该时间所在月的第一天
	public static Date getMonthFirstDay(Date date) {
		final Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

		return calendar.getTime();
	}

	// 传入一个时间，得到该时间所在月的最后一天
	public static Date getMonthLastDay(Date date) {
		final Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

		return calendar.getTime();
	}
	
	// 传入一个时间，得到该时间的前一天
	public static Date getPreDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return  calendar.getTime();
	}
	
	// 传入一个时间，得到当前时间
		public static Date getNowDay(Date date) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return  calendar.getTime();
		}
	
	// 传入一个时间，得到该日期的前半个月
	public static Date getPreFifteenDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -15);
		return  calendar.getTime();
	}
	
	public static Date getPreQueryDatebox(Date date) {
		return getPreFifteenDay(date);
	}
	

	/**
	 * 得到下几天
	 *
	 * @param tsDate
	 */
	public static java.util.Date getNextDate(java.util.Date tsDate, int nDay) {
		if (null == tsDate) {
			return null;
		}
		final GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(tsDate);
		calendar.add(Calendar.DATE, nDay);
		final java.util.Date resDate = calendar.getTime();
		return resDate;
	}

	/**
	 * 得到下几年
	 *
	 * @param tsDate
	 */
	static public java.util.Date getNextYear(java.util.Date tsDate, int nYear) {
		if (null == tsDate) {
			return null;
		}
		final GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(tsDate);
		calendar.add(Calendar.YEAR, nYear);
		final java.util.Date resDate = calendar.getTime();
		return resDate;
	}

	/**
	 * 取两时间段的天数,例如：2003-12-02到2003-12-01为一天
	 *
	 * @param dtBegin
	 *            开始日期(Date类型)
	 * @param dtEnd
	 *            结束日期(Date类型)
	 * @return
	 */
	public static long getTime(java.sql.Date dtBegin, java.sql.Date dtEnd) {
		final Calendar temp = Calendar.getInstance();
		temp.setTime(dtBegin);
		temp.set(Calendar.HOUR, 0);
		temp.set(Calendar.MINUTE, 0);
		temp.set(Calendar.SECOND, 0);
		temp.set(Calendar.MILLISECOND, 0);
		final long lBegin = temp.getTime().getTime();
		temp.setTime(dtEnd);
		temp.set(Calendar.HOUR, 0);
		temp.set(Calendar.MINUTE, 0);
		temp.set(Calendar.SECOND, 0);
		temp.set(Calendar.MILLISECOND, 0);
		final long lEnd = temp.getTime().getTime();
		final long lTime = (lEnd - lBegin) / (24 * 60 * 60 * 1000);
		return lTime;
	}

	/**
	 * 取两时间段的天数,例如：2003-12-02到2003-12-01为一天
	 *
	 * @param dtBegin
	 *            开始日期(Date类型)
	 * @param dtEnd
	 *            结束日期(Date类型)
	 * @return
	 */
	public static long getTime(java.util.Date dtBegin, java.util.Date dtEnd) {
		final Calendar temp = Calendar.getInstance();
		temp.setTime(dtBegin);
		temp.set(Calendar.HOUR, 0);
		temp.set(Calendar.MINUTE, 0);
		temp.set(Calendar.SECOND, 0);
		temp.set(Calendar.MILLISECOND, 0);
		final long lBegin = temp.getTime().getTime();
		temp.setTime(dtEnd);
		temp.set(Calendar.HOUR, 0);
		temp.set(Calendar.MINUTE, 0);
		temp.set(Calendar.SECOND, 0);
		temp.set(Calendar.MILLISECOND, 0);
		final long lEnd = temp.getTime().getTime();
		final long lTime = (lEnd - lBegin) / (24 * 60 * 60 * 1000);
		return lTime;
	}

	/**
	 * 获得字符串的字节长度，其中一个汉字长度为2
	 *
	 * @param s
	 * @return
	 */
	static public int getWordCount(String s) {

		int length = 0;
		if (s != null) {
			for (int i = 0; i < s.length(); i++) {
				final int ascii = Character.codePointAt(s, i);
				if (ascii >= 0 && ascii <= 255) {
					length++;
				} else {
					length += 2;
				}

			}
		}

		return length;

	}

	/**
	 * 获得年份列表
	 *
	 * @return
	 */

	public static List<Integer> getYearList() {
		final List<Integer> list = new ArrayList<Integer>();
		final Integer curYear = getCurYear();

		for (int i = 10; i > 0; i--) {
			list.add(new Integer((curYear.intValue() - i)));

		}
		for (int i = 0; i < 11; i++) {
			list.add(new Integer((curYear.intValue() + i)));
		}
		return list;

	}

	/**
	 * 是否包含汉字
	 *
	 * @param s
	 * @return
	 */
	static public boolean hasChinese(String s) {

		boolean has = false;
		if (s != null) {
			for (int i = 0; i < s.length(); i++) {
				final int ascii = Character.codePointAt(s, i);
				if (!(ascii >= 0 && ascii <= 255)) {
					has = true;
				}

			}
		}
		return has;

	}

	public static String leftStr(String source, int maxByteLen) {
		return leftStr(source, maxByteLen, -1);
	}

	/**
	 * Java中1个char类型的变量可存储任意编码的1个字符，如1个ASC码和或1个中文字符，
	 * 例如：含有3个ASC和含有3个汉字字符的字符串长度是一样的： "1ac".length()==3; "你好a".length()=3;
	 * 但上述两个字符串所占的byte是不一样的，前者是3，后者是5（1个汉字2byte）。 请编写函数: public static String
	 * leftStr(String source, int maxByteLen) 从source中取最大maxByteLen个byte的子串。
	 * 当最后一个byte恰好为一个汉字的前半个字节时，舍弃此byte。例如： String str="我LRW爱JAVA";
	 * leftStr(str,1,-1)==""; leftStr(str,2,-1)=="我"; leftStr(str,4,-1)=="我LR";
	 * leftStr(str,11,-1)=="我LRW"; 当最后一个byte恰好为一个汉字的前半个字节时，补全汉字（多取一个字节）。例如：
	 * String str="我LRW爱JAVA"; leftStr(str,1,1)=="我"; leftStr(str,2,1)=="我";
	 * leftStr(str,4,1)=="我LR"; leftStr(str,11,1)=="我LRW爱";
	 *
	 * @param source
	 *            原始字符串
	 * @param maxByteLen
	 *            截取的字节数
	 * @param flag
	 *            表示处理汉字的方式。1表示遇到半个汉字时补全，-1表示遇到半个汉字时舍弃
	 * @return 截取后的字符串
	 */

	public static String leftStr(String source, int maxByteLen, int flag) {
		String returnStr = "";
		if (source != null && maxByteLen > 0) {

			final byte[] bStr = source.getBytes();
			if (maxByteLen >= bStr.length) {
				returnStr = source;
			} else {
				final String cStr = new String(bStr, maxByteLen - 1, 2);
				if (cStr.length() == 1 && source.contains(cStr)) {
					maxByteLen += flag;
				}
				returnStr = new String(bStr, 0, maxByteLen);
			}

		}
		return returnStr;
	}

	public static void main(String[] args) {
		try {
			// System.out.println(DataFormat.parseDate("", 1));
			// System.out.println(DataFormat.getCurrentUpperDate());
			// System.out.println(DataFormat.leftStr("8我的erqwr",4,-1));
			// System.out.println(DataFormat.leftStr("8我的erqwr",-1,-1));
			// System.out.println(DataFormat.leftStr("",4,-1));
			// System.out.println(DataFormat.leftStr("8我的erqwr",4,1));
			//
			System.out.println(formatNumberSerNo("IF00200001-", "0", 1234, 3));
			System.out.println(("8我的erqwr").substring(0, 8));

			// System.out.println(DataFormat.bSubString("8我的erqwr",0,4,-1));
			// //8我
			// System.out.println(DataFormat.bSubString("8我的erqwr",0,-1,-1)); //
			// System.out.println(DataFormat.bSubString("",0,4,-1));
			// System.out.println(DataFormat.bSubString("8我的erqwr",0,4,1)); //8我

			// System.out.println(DataFormat.bSubString("8我的erqwr",0,5,-1));
			// //8我
			// System.out.println(DataFormat.bSubString("8我的erqwr",1,4,-1)); //我
			// System.out.println(DataFormat.bSubString("8我的erqwr",2,5,-1)); //我
			// System.out.println(DataFormat.bSubString("8我的erqwr",0,5,1));
			// //8我的
			// System.out.println(DataFormat.bSubString("8我的erqwr",1,5,1)); //我的
			// System.out.println(DataFormat.bSubString("8我的erqwr",2,4,1)); //
			// System.out.println(DataFormat.bSubString("8我的erqwr",2,5,1)); //的

			final String tempStr = "A2.09877777777620110913094149119877777777611091311100010001                    1                              Z0000000000";
			System.out.println(DataFormat.bSubString(tempStr, 0, 10, -1)); // 8我的erq日
			System.out.println(DataFormat.bSubString(tempStr, 0, 9, -1)); // 8我的erq
			System.out.println(DataFormat.bSubString(tempStr, 2, 11, -1)); // 我的erq日
			System.out.println(DataFormat.bSubString(tempStr, 2, 10, 1)); // 的erq日
			System.out.println(DataFormat.bSubString(tempStr, 2, 11, 1)); // 的erq日
			System.out.println(DataFormat.bSubString(tempStr, 1, 10, 1)); // 我的erq日
			System.out.println(DataFormat.bSubString("8我的erq日", 1, 11, 1)); // 我的erq日
			final String ff = "8我的erq日d饿";
			System.out.println(ff.length() > 8 ? ff.substring(0, 8) : ff); // 我的erq日

			System.out.println(DataFormat.parseDate("14/02/15", DataFormat.DT_YYIMMIDD));
			System.out.println(DataFormat.parseDate("2013/02/15", DataFormat.DT_YYYYIMMIDD));

			// System.out.println(DataFormat.hasChinese("asdf7*&^433"));
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @throws ParseException
	 *             功能：用于不同种格式的转换Timestamp
	 */
	public static Date parseDate(String timeStr, int type) throws ServiceException {
		Date date = null;
		final SimpleDateFormat dateFormat = new SimpleDateFormat();

		try {
			if (timeStr == null || timeStr.equals("")) {
				return null;
			}

			dateFormat.applyPattern(DataFormat.getDateConvertString(type));
			date = new Date(dateFormat.parse(timeStr).getTime());
		} catch (final Exception e) {
			e.printStackTrace();
			throw new ServiceException("转换date日期错误");
		}
		return date;
	}

	/**
	 * 解析格式化的字符串，转化为数值，例如：12,345.00转化为12345
	 *
	 * @param text
	 *            被格式化的数值
	 * @return
	 */
	public static double parseNumber(String text) {
		int index = text.indexOf(",");
		String sbNumber = "";
		while (index != -1) {
			sbNumber += text.substring(0, index);
			text = text.substring(index + 1, text.length());
			index = text.indexOf(",");
		}
		sbNumber += text;
		return Double.parseDouble(sbNumber);
	}

	/**
	 * @throws ParseException
	 *             功能：用于不同种格式的转换Timestamp
	 */
	public static Timestamp parseTimestamp(String timeStr, int type) throws ServiceException {
		Timestamp timestamp = null;
		final SimpleDateFormat dateFormat = new SimpleDateFormat();

		try {
			if (timeStr == null || timeStr.equals("")) {
				return null;
			}

			dateFormat.applyPattern(DataFormat.getDateConvertString(type));
			timestamp = new Timestamp(dateFormat.parse(timeStr).getTime());
		} catch (final Exception e) {
			e.printStackTrace();
			throw new ServiceException("转换Timestamp日期错误");
		}
		return timestamp;
	}

	public static String rightStr(String source, int beginIndex) {
		return rightStr(source, beginIndex, -1);
	}

	public static String rightStr(String source, int beginIndex, int flag) {
		String returnStr = "";

		if (source != null && beginIndex >= 0) {
			final byte[] bStr = source.getBytes();
			if (beginIndex < bStr.length) {
				if (beginIndex > 0) {
					final String beginStr = new String(bStr, beginIndex - 1, 2);
					if (beginStr.length() == 1 && source.contains(beginStr)) {
						beginIndex += flag;
					}
				}

				returnStr = new String(bStr, beginIndex, bStr.length - beginIndex);

			}

		}

		return returnStr;
	}
	
	public static String toDateTimeString(java.sql.Timestamp tsVar)
	{

		return getDateConvertString(tsVar, DT_YYYYMMDD_HHMMSS);
	}
}
